/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.utbm.tx52.stamp.ui.actionprovider;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.ActionProvider;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import fr.utbm.tx52.stamp.R;
import fr.utbm.tx52.stamp.ui.activity.Viewer;
import fr.utbm.tx52.stamp.ui.fragment.Gallery;

/**
 * ActionProvider which provide options to sort the image gallery
 *
 * @author GREA Antoine
 * @author TISSERAND Geoffrey
 */
public class SortOrder extends ActionProvider implements MenuItem.OnMenuItemClickListener
{

	private Activity activity;
	private MenuItem asc;
	private MenuItem desc;

	public SortOrder(Context activity)
	{
		super(activity);
		this.activity = (Activity) activity;
	}

	@Override
	public View onCreateActionView()
	{
		Log.d(this.getClass().getSimpleName(), "onCreateActionView");
		return null;
	}

	@Override
	public boolean onPerformDefaultAction()
	{
		Log.d(this.getClass().getSimpleName(), "onPerformDefaultAction");
		return super.onPerformDefaultAction();
	}

	@Override
	public boolean hasSubMenu()
	{
		Log.d(this.getClass().getSimpleName(), "hasSubMenu");
		return true;
	}

	@Override
	public void onPrepareSubMenu(SubMenu subMenu)
	{
		Log.d(this.getClass().getSimpleName(), "onPrepareSubMenu");
		subMenu.clear();
		asc = subMenu.add(R.string.sort_order_asc);
		asc.setOnMenuItemClickListener(this);
		desc = subMenu.add(R.string.sort_order_desc);
		desc.setOnMenuItemClickListener(this);
	}

	@Override
	public boolean onMenuItemClick(MenuItem item)
	{
		boolean ascendent = item.getOrder() == 0;
		switch (Gallery.sortType)
		{
			case BY_DATE:
				if (Gallery.sort != null)
				{
					Gallery.sort.sortByDate(ascendent);
				}
				if (Viewer.sort != null)
				{
					Viewer.sort.sortByDate(ascendent);
				}
				break;
			case BY_CITY:
				if (Gallery.sort != null)
				{
					Gallery.sort.sortByCity(ascendent);
				}
				if (Viewer.sort != null)
				{
					Viewer.sort.sortByCity(ascendent);
				}
				break;
		}
		if (Gallery.instance != null)
		{
			Gallery.instance.imageAdapter.notifyDataSetChanged();
		}
		if (Viewer.instance != null)
		{
			Viewer.instance.imageAdapter.notifyDataSetChanged();
		}
		return true;
	}
}
